/**
 * Development Solutions (DS) Software
 * Created on 13-may-2011
 * @author Fernando Gil
 * lobho.gil@gmail.com
 *
 * Last Modified 16-may-2011
 * By: Fernando Gil
 *
 */
package View;

import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class Subnets extends javax.swing.JFrame {

    private int clase;// 1=A,2=B,3=C
    private int mask; // 10,11 ... 30
    private int host; // host x red
    private DefaultTableModel model;
    private About ab;
    private ImageIcon icQuestion;
    private String options[]={"No","Yes"};
    private String option[]={"OK"};

    /** Creates new form Subnets */


    public Subnets(String num1, String num2, String num3, int bits, int bitsNet) {
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
        }
        initComponents();
        setIconImage(new javax.swing.ImageIcon(getClass().getResource("/Images/DSSoftware.png")).getImage());
        icQuestion = new ImageIcon(getClass().getResource("/Images/DSSQuestion48.png"));
        this.setLocationRelativeTo(null);
        this.calculateSubnets(num1, num2, num3, bits, bitsNet);

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextArea();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();

        setTitle("Subnets");

        jPanel1.setBackground(new java.awt.Color(221, 217, 210));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Subnets"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setText("Subnet mask:  255.255.255.0");

        jButton1.setText("View details");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextPane1.setColumns(20);
        jTextPane1.setRows(5);
        jScrollPane2.setViewportView(jTextPane1);

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                        .add(298, 298, 298))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 201, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 256, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(20, 20, 20))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(23, 23, 23)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 277, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton1)))
                .addContainerGap())
        );

        jMenu1.setText("Subnet Calculator");

        jMenuItem2.setText("About");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuItem3.setText("Exit");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String text = "";
        if (clase == 3) {
            int rows[] = this.jTable1.getSelectedRows();
            for (int i = 0; i < rows.length; i++) {
                String subnetAdd = this.jTable1.getValueAt(rows[i], 0).toString();
                String subnetAdd2 = subnetAdd.substring(0, subnetAdd.lastIndexOf(".") + 1);
                //Sacar el último numero de la ip
                int octeto = Integer.parseInt(subnetAdd.substring(subnetAdd.lastIndexOf(".") + 1));
                text += "Subnet    address:  " + subnetAdd + "\n"
                        + "1st  host address:  " + subnetAdd2 + (octeto + 1) + "\n"
                        + "Last host address:  " + subnetAdd2 + (octeto + host) + "\n"
                        + "Subnet  Broadcast:  " + subnetAdd2 + (octeto + host + 1) + "\n\n\n";
            }
        } else if (clase == 2) {
            if (mask == 24) {
                int rows[] = this.jTable1.getSelectedRows();
                for (int i = 0; i < rows.length; i++) {
                    String subnetAdd = this.jTable1.getValueAt(rows[i], 0).toString();
                    String subnetAdd2 = subnetAdd.substring(0, subnetAdd.lastIndexOf(".") + 1);
                    //Sacar el último numero de la ip
                    int octeto = Integer.parseInt(subnetAdd.substring(subnetAdd.lastIndexOf(".") + 1));
                    text += "Subnet    address:  " + subnetAdd + "\n"
                            + "1st  host address:  " + subnetAdd2 + (octeto + 1) + "\n"
                            + "Last host address:  " + subnetAdd2 + (octeto + host) + "\n"
                            + "Subnet  Broadcast:  " + subnetAdd2 + (octeto + host + 1) + "\n\n\n";
                }

            } else if (mask < 24) {
                int rows[] = this.jTable1.getSelectedRows();
                if (rows.length > 0) {
                    String octeto3 = jTable1.getValueAt(0, 0).toString();
                    octeto3 = octeto3.substring(0, octeto3.lastIndexOf("."));
                    String net = octeto3.substring(0, octeto3.lastIndexOf(".")) + ".";
                    octeto3 = octeto3.substring(octeto3.lastIndexOf(".") + 1);
                    int hosts = Integer.parseInt(octeto3);
                    for (int i = 0; i < rows.length; i++) {
                        String subnetAdd = this.jTable1.getValueAt(rows[i], 0).toString();
                        String subnetAdd2 = subnetAdd.substring(0, subnetAdd.lastIndexOf("."));
                        //Sacar el penúltimo numero de la ip
                        String oct = subnetAdd2.substring(subnetAdd2.lastIndexOf(".") + 1);
                        int octeto = Integer.parseInt(oct);
                        text += "Subnet    address:  " + net + octeto + ".0" + "\n"
                                + "1st  host address:  " + net + octeto + ".1" + "\n"
                                + "Last host address:  " + net + (hosts + octeto - 1) + ".254" + "\n"
                                + "Subnet  Broadcast:  " + net + (hosts + octeto - 1) + ".255" + "\n\n\n";
                    }
                }
            } else if (mask > 24) {
                int row = this.jTable1.getSelectedRow();
                /** En este caso cada red de la tabla contiene más subredes dentro de ella
                por lo que en estta ocación solamente dejaremos que se seleccione una red
                de la tabla, para poder describir, cada subred contenida, en el area de texto*/
                String red = this.jTable1.getValueAt(row, 0).toString();
                red = red.substring(0, red.lastIndexOf(".")); // ejemplo 180.34.2
                int octeto3 = Integer.parseInt( red.substring(red.lastIndexOf(".")+1)) ; //tomar el ultimo octeto
                red+=".";
                //System.out.println(octeto3);
                int bitsHost = 32-mask;
                int bitsRed = mask-24;
                int numSubredes = (int)Math.pow(2, bitsRed);
                int hostxNet = (int)Math.pow(2, bitsHost);
                /*System.out.println(bitsHost);
                System.out.println(bitsRed);
                System.out.println(hostxNet);
                 */

                //Calcular ceros a la derecha
                String zeros="";
                for (int i = bitsRed; i < 8; i++) {
                    zeros += "0";
                }
                //System.out.println(zeros);
                //Determinar las subredes
                int max = (int) Math.pow(2, bitsRed);
                String binary, subnet;
                ArrayList <String >numeros = new ArrayList();
                int init = 0;
                int rest = 0;
                if(octeto3 == 0)
                    init = 1;
                if(octeto3 == 255)
                    rest = 1;
                for (int i = init; i < max-rest; i++) {
                    //Convertir la i a binario
                    binary = Integer.toBinaryString(i);
                    subnet = Integer.parseInt(binary+zeros, 2)+"";
                    //System.out.println(i+":  "+subnet);
                    numeros.add(subnet);
                }

                System.out.println("numero de host x red: "+hostxNet);
                /**Por cada subred calculada, representar los hosts*/
                for(int i=0;i<numeros.size();i++){
                    //System.out.println(red+numeros.get(i));
                    int octeto = Integer.parseInt(numeros.get(i));
                        text +=   "Subnet    address:  " + red + octeto  + "\n"
                                + "1st  host address:  " + red + (octeto+1)  + "\n"
                                + "Last host address:  " + red + (hostxNet + octeto-2) +"\n"
                                + "Subnet  Broadcast:  " + red + (hostxNet + octeto-1) +"\n\n\n";
                    /* Por cada subred describirla*/
                    //System.out.println("Subred "+i+" : "+red);
                }

            }

        } else if (clase == 1) {
            //CLASE A!!
            int bitsHost = 32-mask;
            int bitsRed = mask-8;
            int numSubredes = (int)Math.pow(2, bitsRed)-2;
            int hostxNet = (int)Math.pow(2, bitsHost)-2;
            
            if(mask<16){
                int rows[] = this.jTable1.getSelectedRows();
                if (rows.length > 0) {
                    String octeto2 = jTable1.getValueAt(0, 0).toString();
                    octeto2 = octeto2.substring(0, octeto2.lastIndexOf("."));
                    String net = octeto2.substring(0, octeto2.lastIndexOf("."));
                    octeto2 = net.substring(net.lastIndexOf(".") + 1);
                    net = net.substring(0,net.lastIndexOf(".")+1);
                    int hosts = Integer.parseInt(octeto2);

                    for (int i = 0; i < rows.length; i++) {
                        String subnetAdd = this.jTable1.getValueAt(rows[i], 0).toString();
                        String subnetAdd2 = subnetAdd.substring(0, subnetAdd.lastIndexOf("."));
                        subnetAdd2 = subnetAdd2.substring(0, subnetAdd2.lastIndexOf("."));
                        //Sacar el segundo octeto
                        String oct = subnetAdd2.substring(subnetAdd2.lastIndexOf(".") + 1);
                        //System.out.println("OCTeto2: "+oct);
                        int octeto = Integer.parseInt(oct);
                        text += "Subnet    address:  " + net + octeto + ".0.0" + "\n"
                                + "1st  host address:  " + net + octeto + ".0.1" + "\n"
                                + "Last host address:  " + net + (hosts + octeto - 1) + ".255.254" + "\n"
                                + "Subnet  Broadcast:  " + net + (hosts + octeto - 1) + ".255.255" + "\n\n\n";
                    }
                }
            }else if(mask==16){
                int []rows = this.jTable1.getSelectedRows();
                if (rows.length > 0) {
                   for (int i=0;i<rows.length;i++){
                       String red = this.jTable1.getValueAt(rows[i], 0).toString();
                       red = red.substring(0,red.lastIndexOf("."));
                       red = red.substring(0,red.lastIndexOf(".")+1); //10.9.

                       System.out.println(red);
                       text +=   "Subnet    address:  " + jTable1.getValueAt(rows[i], 0).toString()  + "\n"
                                    + "1st  host address:  " + red + "0.1"  + "\n"
                                    + "Last host address:  " + red + "255.254" +"\n"
                                    + "Subnet  Broadcast:  " + red + "255.255" +"\n\n\n";

                   }
                }
               /*
                String red = this.jTable1.getValueAt(rows[i], 0).toString();
                   red = red.substring(0,red.lastIndexOf("."));
                   red = red.substring(0,red.lastIndexOf("."));
                   String octeto2 = red.substring(red.indexOf("."));
                   red = red.substring(0,red.lastIndexOf("."));
                   System.out.println(octeto2);
                   System.out.println(red);
                */
            }else if(mask>16){

                if (mask < 24) {

                   int row = this.jTable1.getSelectedRow();
                   String red = this.jTable1.getValueAt(row, 0).toString();
                   /*Sacar el segundo octeto*/
                   red = red.replace('.', '-');
                   //System.out.println(red);
                   String octetos[]=red.split("-");
                   String octeto2 = (octetos[1]);
                   red = red.replace('-', '.');
                   //System.out.println(red);
                   //Calcular ceros a la derecha
                    //System.out.println(bitsRed);
                    String zeros="";
                    for (int i = bitsRed; i < 16; i++) {
                        zeros += "0";
                    }
                    //System.out.println(zeros);
                    //Determinar las subredes
                    int max = (int) Math.pow(2, bitsRed-8);
                    String binary, subnet;
                    ArrayList <String >numeros = new ArrayList();
                    System.out.println(octeto2);

                   int init = 0;
                   int rest = 0;
                   if(octeto2.equals("0"))
                       init =1;
                   if(octeto2.equals("255"))
                       rest =1;

                    for (int i = init; i < max-rest; i++) {
                        //Convertir la i a binario
                        binary = Integer.toBinaryString(i);
                        subnet = Integer.parseInt(binary+zeros, 2)+"";
                        System.out.println(i+":  "+subnet);
                        numeros.add(subnet);
                    }

                   int pHost = 24-mask;
                   int hosts = (int)Math.pow(2,pHost)-1;
                   System.out.println(hosts);
                   /*Con la porcion de Host calculada podemos determinar el increemento
                    para la descripcion de las subredes*/

                   red = octetos[0]+"."+octetos[1]+".";
                   //System.out.println(red);
                   for(int i=0;i<numeros.size();i++){
                        String subred = red + numeros.get(i);
                        int num =  Integer.parseInt(numeros.get(i));
                        text +=   "Subnet    address:  " + subred + ".0" + "\n"
                                    + "1st  host address:  " + subred + ".1"  + "\n"
                                    + "Last host address:  " + red+(num+hosts) + ".254" +"\n"
                                    + "Subnet  Broadcast:  " + red+(num+hosts) + ".255" +"\n\n\n";
                   }
                   
                }else if (mask == 24) {
                   int row = this.jTable1.getSelectedRow();
                   String red = this.jTable1.getValueAt(row, 0).toString();
                   /*Sacar el segundo octeto*/
                   red = red.replace('.', '-');
                   //System.out.println(red);
                   String octetos[]=red.split("-");
                   String octeto2 = (octetos[1]);
                   red = red.replace('-', '.');
                   //System.out.println(red);

                   int init = 0;
                   int rest = 0;
                   if(octeto2.equals("0"))
                       init =1;
                   if(octeto2.equals("255"))
                       rest =1;

                   red = octetos[0]+"."+octetos[1]+".";
                   /* Recorrer las 256 o 255 subredes segun el caso
                      Por cada subred especificar sus datos*/
                   for(int i=0+init;i<256-rest;i++){
                        String subred = red+ i ;
                        text +=   "Subnet    address:  " + subred + ".0" + "\n"
                                    + "1st  host address:  " + subred + ".1"  + "\n"
                                    + "Last host address:  " + subred + ".254" +"\n"
                                    + "Subnet  Broadcast:  " + subred + ".255" +"\n\n\n";

                   
                   }
                }if (mask > 24) {
                   int row = this.jTable1.getSelectedRow();
                   String red = this.jTable1.getValueAt(row, 0).toString();
                   /*Sacar el segundo octeto*/
                   red = red.replace('.', '-');
                   String octetos[]=red.split("-");
                   red = red.replace('-', '.');
                   String octeto2 = (octetos[1]);
                   red = octetos[0]+"."+octetos[1]+"."+0+".";
                   int pHost = 32-mask;
                   int hosts = (int)Math.pow(2,pHost)-1;
                   //System.out.println(red);
                   //System.out.println(hosts);
                   String zeros="";
                    for (int i = 0; i < pHost; i++) {
                        zeros += "0";
                    }
                    System.out.println(zeros);
                    //Determinar las subredes
                    int max = (int) Math.pow(2, 8-pHost);
                    String binary, subnet;
                    ArrayList <String >numeros = new ArrayList();

                   int init = 0;
                   int rest = 0;
                   if(octeto2.equals("0"))
                       init =1;

                    for (int i = init; i < max-rest; i++) {
                        //Convertir la i a binario
                        binary = Integer.toBinaryString(i);
                        subnet = Integer.parseInt(binary+zeros, 2)+"";
                        System.out.println(i+":  "+subnet);
                        numeros.add(subnet);
                    }

                    red = octetos[0]+"."+octetos[1]+".0.";
                   //System.out.println(red);
                   for(int i=0;i<numeros.size();i++){
                        int num =  Integer.parseInt(numeros.get(i));
                        text +=   "Subnet    address:  " + red+num  + "\n"
                                    + "1st  host address:  " + red+(num+1)  + "\n"
                                    + "Last host address:  " + red+(num+hosts-1)  +"\n"
                                    + "Subnet  Broadcast:  " + red+(num+hosts)  +"\n\n";
                   }
                   text +=            "Continue increasing third octet\n\n";
                   red = octetos[0]+"."+octetos[1]+".255.";
                   //System.out.println(red);
                   
                   numeros = new ArrayList();
                   init = 0;
                   rest = 0;
                   if(octeto2.equals("255"))
                       rest =1;
                   for (int i = init; i < max-rest; i++) {
                        //Convertir la i a binario
                        binary = Integer.toBinaryString(i);
                        subnet = Integer.parseInt(binary+zeros, 2)+"";
                        System.out.println(i+":  "+subnet);
                        numeros.add(subnet);
                    }
                   for(int i=0;i<numeros.size();i++){
                        int num =  Integer.parseInt(numeros.get(i));
                        text +=   "Subnet    address:  " + red+num  + "\n"
                                    + "1st  host address:  " + red+(num+1)  + "\n"
                                    + "Last host address:  " + red+(num+hosts-1) +"\n"
                                    + "Subnet  Broadcast:  " + red+(num+hosts) +"\n\n";
                   }





                }

            }

        }
        this.jTextPane1.setText(text);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        int opc= JOptionPane.showOptionDialog(null, "Do you really want to exit? ","Warning",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE,icQuestion,options,options[1]);
        if(opc==1)
            System.exit(0);
    }//GEN-LAST:event_jMenuItem3ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        if(ab==null){
            Thread t = new Thread(
                    new Runnable() {
                        public void run() {
                           ab = new About();
                           ab.setVisible(true);
                        }
                    });
            t.start();
        }else{
            /* Primero reasigno la posicion del Frame en relacion al Menú
             y después lo hacemos Visivle.*/
            ab.setVisible(true);
        }
    }//GEN-LAST:event_jMenuItem2ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea jTextPane1;
    // End of variables declaration//GEN-END:variables

    public void calculateSubnets(String num1, String num2, String num3, int bits, int bitsNet) {
        clase = 0;
        mask = bits + bitsNet;
        jLabel1.setText("Mask: " + this.calculateMask(bits + bitsNet) + "  /" + mask);
        model = new DefaultTableModel();
        model.addColumn("Subnets");
        int pHost = (32 - (bits + bitsNet));
        
        /*
        String net = num1+"."+num2+"."+num3;
        System.out.println("IP: "+net+".0");
        System.out.println("Bits de Red: "+bits);
        System.out.println("Bits de Subred: "+bitsNet);
        System.out.println("Porcion de Red: "+(bits+bitsNet));
        System.out.println("Poricion de Host: "+pHost);*/

        String zeros = "";
        if (bits < 9) {
            //CLASE A
            clase = 1;
            /* Evaluar si el tamaño de los bits de subred son menores a 8.
            Si son menores a 8 significa que solo tendremos que hacer un
            solo ciclo para calcular las subredes.*/
            int init = 1;
            int res = 1;
            if (bitsNet > 8) {
                /*Si los bits para subnet sobrepasan los 8
                entonces solo se mostraran las primeras 256 subredes*/
                bitsNet = 8;
                init = 0;
                res=0;
                this.jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);

            }
            host = (int) Math.pow(2, pHost) - 2;
            //Calcualar ceros a la derecha
            String net = num1 + ".";
            for (int i = bitsNet; i < 8; i++) {
                zeros += "0";
            }
            //Determinar las subredes
            String binary, subnet;
            int max = (int) Math.pow(2, bitsNet) - res;
            for (int i = init; i < max; i++) {
                binary = Integer.toBinaryString(i) + zeros;
                subnet = net + Integer.parseInt(binary, 2) + ".0.0";
                String[] row = {subnet};
                model.addRow(row);
            }
            this.jTable1.setModel(model);
        } else if (bits < 17) {
            //CLASE B
            clase = 2;
            /* Evaluar si el tamaño de los bits de subred son menores a 8.
            Si son menores a 8 significa que solo tendremos que hacer un
            solo ciclo para calcular las subredes.*/
            int init = 1;
            int res = 1;
            if (bitsNet > 8) {
                /*Si los bits para subnet sobrepasan los 8
                entonces solo se mostraran las primeras 256 subredes*/
                bitsNet = 8;
                init = 0;
                res = 0;
                this.jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);  //asignar seleccion simple
                
            }
            host = (int) Math.pow(2, pHost) - 2;
            //Calcualar ceros a la derecha
            String net = num1 + "." + num2 + ".";
            for (int i = bitsNet; i < 8; i++) {
                zeros += "0";
            }
            //Determinar las subredes
            String binary, subnet;
            int max = (int) Math.pow(2, bitsNet) - res;
            for (int i = init; i < max; i++) {
                binary = Integer.toBinaryString(i) + zeros;
                subnet = net + Integer.parseInt(binary, 2) + ".0";
                String[] row = {subnet};
                model.addRow(row);
            }
            this.jTable1.setModel(model);

        } else {
            //CLASE C -- LISTA!!
            clase = 3;
            host = (int) Math.pow(2, pHost) - 2;
            //Calcualar ceros a la derecha
            String net = num1 + "." + num2 + "." + num3 + ".";
            for (int i = bitsNet; i < 8; i++) {
                zeros += "0";
            }
            //Determinar las subredes
            String binary, subnet;
            int max = (int) Math.pow(2, bitsNet) - 1;
            for (int i = 1; i < max; i++) {
                binary = Integer.toBinaryString(i) + zeros;
                subnet = net + Integer.parseInt(binary, 2);
                String[] row = {subnet};
                model.addRow(row);
            }
            this.jTable1.setModel(model);
        }

    }

    private String calculateMask(int bits) {
        String mask = "";
        if (bits == 10) {
            mask = "255.192.0.0";
        } else if (bits == 11) {
            mask = "255.224.0.0";
        } else if (bits == 12) {
            mask = "255.240.0.0";
        } else if (bits == 13) {
            mask = "255.248.0.0";
        } else if (bits == 14) {
            mask = "255.252.0.0";
        } else if (bits == 15) {
            mask = "255.254.0.0";
        } else if (bits == 16) {
            mask = "255.255.0.0";
        } else if (bits == 17) {
            mask = "255.255.128.0";
        } else if (bits == 18) {
            mask = "255.255.192.0";
        } else if (bits == 19) {
            mask = "255.255.224.0";
        } else if (bits == 20) {
            mask = "255.255.240.0";
        } else if (bits == 21) {
            mask = "255.255.248.0";
        } else if (bits == 22) {
            mask = "255.255.252.0";
        } else if (bits == 23) {
            mask = "255.255.254.0";
        } else if (bits == 24) {
            mask = "255.255.255.0";
        } else if (bits == 25) {
            mask = "255.255.255.128";
        } else if (bits == 26) {
            mask = "255.255.255.192";
        } else if (bits == 27) {
            mask = "255.255.255.224";
        } else if (bits == 28) {
            mask = "255.255.255.240";
        } else if (bits == 29) {
            mask = "255.255.255.248";
        } else if (bits == 30) {
            mask = "255.255.255.252";
        }

        return mask;
    }
}
